package com.power.ding.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import com.plf.common.domain.PageDataResponse;
import com.plf.common.domain.ResponseDTO;
import com.plf.common.utils.ExcelUtil;
import com.power.ding.model.Di_user;
import com.power.ding.model.pojo.bo.Di_rel_dept_BaseUser;
import com.power.ding.service.Di_userService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.power.ding.model.Di_rel_dept_correct;
import com.power.ding.model.pageInfo.Di_rel_dept_correctPageInfo;
import com.power.ding.api.IDi_rel_dept_correctFeign;
import com.power.ding.service.Di_rel_dept_correctService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 司法社矫组织机构表控制器
 */

//@Slf4j
//@PowerLog
@RestController
@Api(value = "Di_rel_dept_correctController", tags = {"司法社矫组织机构表"})
public class Di_rel_dept_correctController implements IDi_rel_dept_correctFeign {

    @Resource
    private Di_rel_dept_correctService di_rel_dept_correctService;

    @Resource
    private Di_userService diUserService;

    @Override
    @ApiOperation("查询司法社矫组织机构表分页列表")
    @SaCheckPermission("di_rel_dept_correct_list")
    public ResponseDTO list(Di_rel_dept_correctPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo参数不能为空");
        Page<Di_rel_dept_correct> page = this.di_rel_dept_correctService.findAll(pageInfo);
        List list = new ArrayList();
        if (!page.isEmpty()) {
            for (Di_rel_dept_correct di_rel_dept_correct : page) {
                di_rel_dept_correct.geneText();
                list.add(di_rel_dept_correct);
            }
        }
        PageDataResponse p = new PageDataResponse(list, page.getTotalElements(), page.getSize(), pageInfo.getPageNumber() <= 1 ? 1 : pageInfo.getPageNumber());
        return ResponseDTO.succData(p);
    }

    @Override
    @ApiOperation("查看司法社矫组织机构表")
    @SaCheckPermission("di_rel_dept_correct_view")
    public ResponseDTO view(String id) {
        Assert.notNull(id, "id不能为空");
        Di_rel_dept_correct di_rel_dept_correct = di_rel_dept_correctService.getById(id);
        Map<String, Object> map = BeanUtil.beanToMap(di_rel_dept_correct);
        return ResponseDTO.succData(map);
    }

    @Override
    @ApiOperation("删除司法社矫组织机构表")
    @SaCheckPermission("di_rel_dept_correct_del")
    public ResponseDTO delete(String id) {
        Assert.notNull(id, "id不能为空");
        di_rel_dept_correctService.removeById(id);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation("保存或修改司法社矫组织机构表")
    @SaCheckPermission("di_rel_dept_correct_save")
    public ResponseDTO save(Di_rel_dept_correct di_rel_dept_correct) {
        Assert.notNull(di_rel_dept_correct, "di_rel_dept_correct不能为空");
        di_rel_dept_correctService.saveOrUpdate(di_rel_dept_correct);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation(value = "批量删除司法社矫组织机构表")
    @SaCheckPermission("di_rel_dept_correct_del")
    public ResponseDTO batchDel(String ids) {
        Assert.isTrue(StringUtils.hasText(ids), "ids不能为空");
        List<Integer> list = Arrays.asList(ids.split(",")).stream().map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
        di_rel_dept_correctService.removeByIds(list);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation(value = "导出excel")
    @SaCheckPermission("di_rel_dept_correct_list")
    public void output(HttpServletResponse response, Di_rel_dept_correctPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo不能为空");
        String title = "司法社矫组织机构表";
        String[] heads = {"序号", "名称", "父节点id", "行政区域id", "关联用户id", "组织类型"};
        String[] fields = {"id", "orgname", "fid", "adid", "contactuser", "orgtype"};
        Page<Di_rel_dept_correct> page = this.di_rel_dept_correctService.findAll(pageInfo);
        ExcelUtil.exportXls(title, heads, fields, page.getContent(), response);
    }

    @Override
    @ApiOperation(value = "导出excel模板")
    public void exportExcelTemplate(HttpServletResponse response) {
        String title = "司法社矫组织机构表";
        //id自增或list从表可自行删除，需与导入项一致
        String[] heads = {"序号", "名称", "父节点id", "行政区域id", "关联用户id", "组织类型"};
        ExcelUtil.exportXls(title, heads, null, null, response);
    }

    @Override
    @ApiOperation(value = "导入excel")
    public ResponseDTO importExcel(MultipartFile file) throws IOException {
        Assert.notNull(file, "file不能为空");
        //id自增或list从表可自行删除，需与下载的模板中导入项一致
        String[] fields = {"id", "orgname", "fid", "adid", "contactuser", "orgtype"};
        List<Di_rel_dept_correct> list = (List<Di_rel_dept_correct>) ExcelUtil.Import(fields, file, Di_rel_dept_correct.class);
        if (!CollectionUtils.isEmpty(list)) {
            this.di_rel_dept_correctService.saveAll(list);
        }
        return ResponseDTO.succ();
    }

    @Override
    public ResponseDTO getQueryTree() {
        List<Map<String, Object>> vo = this.di_rel_dept_correctService.getQueryTree();
        return ResponseDTO.succData(vo);
    }

    @Override
    public ResponseDTO queryPageDeptAndUser(Di_rel_dept_correctPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo参数不能为空");
        PageDataResponse pageDataResponse = this.di_rel_dept_correctService.queryPageDeptAndUser(pageInfo);
        return ResponseDTO.succData(pageDataResponse);
    }

    @Override
    public ResponseDTO queryBaseOrgUserByUser(Di_rel_dept_BaseUser baseUser) {
        return ResponseDTO.succData(this.di_rel_dept_correctService.queryBaseOrgUserByUser(baseUser));
    }

    @Override
    public ResponseDTO queryBaseOrgUser() {
        return ResponseDTO.succData(this.di_rel_dept_correctService.queryBaseOrgUser());
    }

    @Override
    public ResponseDTO queryBaseOrgTree() {
        return ResponseDTO.succData(this.di_rel_dept_correctService.queryBaseOrgTree());
    }

    @Override
    public ResponseDTO queryUserByMobile(String mobile) {
        List<String> list = new ArrayList<>();
        list.add(mobile);
        List<Di_user> userByPhones = diUserService.getUserByPhones(list);
        return ResponseDTO.succData(userByPhones);
    }


    @Override
    public ResponseDTO queryDeptAndUserById(String id) {

        Di_user diUser = this.di_rel_dept_correctService.queryDeptAndUserById(id);
        return ResponseDTO.succData(diUser);
    }

    @Override
    public ResponseDTO updataContactuser(Di_rel_dept_correct di_rel_dept_correct) {
        Assert.notNull(di_rel_dept_correct,"参数不能为空");
        Assert.notNull(di_rel_dept_correct.getId(),"id参数异常");
        Assert.notNull(di_rel_dept_correct.getContactuser(),"用户不能为空");
        this.di_rel_dept_correctService.updataContactuser(di_rel_dept_correct);
        return ResponseDTO.succ();
    }

}
